package com.zlm.tools.controller;

import org.springframework.core.io.Resource;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * {@code }
 *
 * @author zlm
 * @since 2025/7/3
 */
@RestController
public class DownloadTestController {

    private final RestTemplate restTemplate = new RestTemplate();

    /**
     * 测试文件普通下载
     * @param response
     * @param filename
     */
    @GetMapping("/downloadFile")
    public void downloadFile(HttpServletResponse response,String filename) {
        response.setHeader("Content-Disposition", "attachment;filename="+filename);
        response.setHeader("Content-Type", MediaType.APPLICATION_OCTET_STREAM_VALUE);

        ResponseEntity<Resource> responseEntity = restTemplate.getForEntity("http://localhost:8080/download?filename=" + filename, Resource.class);
        if(responseEntity.getStatusCode().is2xxSuccessful() && responseEntity.getBody() != null){
            try(InputStream inputStream = responseEntity.getBody().getInputStream();
                BufferedInputStream bis = new BufferedInputStream(inputStream);
                ServletOutputStream os = response.getOutputStream()
            ) {
                byte[] buffer = new byte[1024*8];
                int len;
                while ((len = bis.read(buffer)) != -1) {
                    os.write(buffer,0,len);
                    System.out.println("download byte length: "+len);
                }
                os.flush();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }

    /**
     * 测试流下载
     * @param response
     * @param filename
     */
    @GetMapping("/downloadStreamFile")
    public void downloadStreamFile(HttpServletResponse response,String filename) {
        response.setHeader("Content-Disposition", "attachment;filename="+filename);
        response.setHeader("Content-Type", MediaType.APPLICATION_OCTET_STREAM_VALUE);
        restTemplate.execute("http://localhost:8080/download?filename="+filename,
                HttpMethod.GET,
                request -> {
                    // 设置请求头
                    request.getHeaders().add("Accept", MediaType.APPLICATION_OCTET_STREAM_VALUE);
                    request.getHeaders().set("Authorization","token");
                },
                resp -> {
                    InputStream inputStream = resp.getBody();
                    BufferedInputStream bis = new BufferedInputStream(inputStream);
                    ServletOutputStream os = response.getOutputStream();
                    byte[] buffer = new byte[1024*8];
                    int len;
                    while ((len = bis.read(buffer)) != -1) {
                        os.write(buffer,0,len);
                        System.out.println("download byte length: "+len);
                    }
                    bis.close();
                    os.flush();
                    os.close();
                    return null;
                });
    }

}
